<template>
	<div>
	<div style="width: 100%;height: 20px;background-color: #ebedf0;"></div>
	<div style="padding: 20px;">
		<transition name="custom-classes-transition" enter-active-class="animated fadeInDown" leave-active-class="animated fadeOutUp" >
			<addteacher 
				:importsubjectid="subjectselect"
				:importsubjectlist="subjectlist"
				:importteacherid="teacherid"
				v-on:chance="chance" v-if="show" style="margin-bottom: 20px;"><span>{{addoredit}}</span></addteacher>
		</transition>
		<Row >
	        <Col span="18" style="text-align: left;margin-top:15px;">
	        	<span>科目筛选：</span>
	        	<RadioGroup  v-model="subjectselect" type="button" @on-change="changeselect">
	        		<Radio v-for="item in subjectlist" :key="item.value" :label="item.value">{{item.label}}</Radio>
			       
			    </RadioGroup>
	        </Col>
	        <Col span="6" style="text-align: right;margin-top:15px;">
	        	<!--<Button type="ghost">导入所有教师</Button>
	        	<Button type="ghost">导入模板下载</Button>-->
	        </Col>
	        <Spin size="large" fix v-if="subjectlistshow"></Spin>
	    </Row>
	    
	    <Row>
	    	<Col span="12" style="text-align: left;margin-top:15px;">
	        	<span style="font-size: 18px;">教师列表：</span>
	        </Col>
	        <Col span="12" style="text-align: right;margin-top:15px;font-size: 16px;">
	        	<Input v-model="searchInfo" icon="ios-search" @on-click="searchTeacher" @on-enter="searchTeacher" placeholder="姓名/账号" style="width: 200px;cursor: pointer;"></Input>
	        	<Tooltip content="导出账号信息" theme="light" placement="top">
		        	<Button style="background-color: #4496fc;" size="small" :loading="loading" @click="exportexl">
		        		<span v-if="!loading"><img style="width: 20px;margin-top: 2px;" src="../../../../static/img/export.png" alt="" /></span>
				        <span v-else>加载中</span>
		        	</Button>
	        	</Tooltip>
	        	<Tooltip content="添加老师" theme="light" placement="top">
	        		<Button @click="addteacher"  type="success" size="small"><i class="iconfont alicon-add1"></i></Button>
	        	</Tooltip>
	        	<Tooltip content="编辑老师" theme="light" placement="top">
	        		<Button @click="editteacher" type="warning" size="small"><i class="iconfont alicon-write"></i></Button>
	        	</Tooltip>
	        	<Tooltip content="删除老师" theme="light" placement="top">
	        		<Button @click="delteacher" type="error" size="small"><i class="iconfont alicon-delete"></i></Button>
	        	</Tooltip>
	        </Col> 
	        
	    </Row>
	    <div style="position: relative;">
	    	<Table style="margin-top: 10px;" border ref="selection" :columns="columns4" :data="data1" @on-selection-change="studentselect"></Table>
	    	<Page style="text-align: center;margin-top:15px;" :current="current" :total="recordCount" @on-change="pageChange"/>
	    	<Spin size="large" fix v-if="tableshow"></Spin>
        </div>
	</div>
	</div>
</template>

<script>
	import api from "@/components/js/Apidomain.js"
	import addteacher from '@/components/performance/basicsetting/Addteacher'
	export default{
		data(){
			return{
				addoredit:"",				//添加面板显示添加还是编辑
				
				show:false,						//添加教师面板的显示和隐藏
				tableshow:false,				//教师列表的加载中显示隐藏
				subjectlistshow:false,			//科目选择列表的加载中显示隐藏
				
				selections:"",					//table选择的集合
				subjectselect:"",				//选择的科目
				teacherid:"",					//编辑老师的id
				subjectlist: [],
				
				searchInfo:"",
				//分页
		        recordCount:0,				//总条数
				current:1,					//页码
				
				loading: false,				//下载模板按钮上的加载中显示隐藏
				
                columns4: [
                    {type: 'selection',width: 60,align: 'center'},
                    {title: '工号',key: 'no',align: 'center',},
                    {title: '账号',key: 'account',align: 'center',},
                    {title: '姓名',key: 'name',align: 'center',},
                    {title: '任课科目',key: 'subject',align: 'center',},
                    {title: '任课班级',key: 'classesInfo',align: 'center',},
                    
                ],
                data1: []
			}
		},
		components:{
			addteacher,
		},
		created(){
			this.getsubjectlist()
		},
		methods:{
			changeselect:function(){
				this.current = 1;
				this.getteacherlist();
			},
			studentselect:function(selection){
           		this.selections = selection
//         		//console.log(this.selections)
           	},
           	addteacher:function(){
           		this.show = true;
           		this.teacherid = "";
           		this.addoredit = "添加老师";
           	},
           	chance:function(){
           		this.show = false;
           		this.current = 1;
           		this.getteacherlist();
           	},
           	editteacher:function(){
           		this.addoredit = "编辑老师"
//         		//console.log(this.selections[0].id)
				if(this.selections.length != 1){
					this.$Message.error({
								content: '请勾选一条信息',
								duration: 10,
								closable: true
							});
					return;
				}
				this.teacherid = this.selections[0].id;
				this.show = true;
				
           	},
           	//获取科目选择列表
           	getsubjectlist:function(){
           		this.subjectlistshow = true;
           		var self = this;
            	let url = api.apidomain+'/tqes/web/tchTeacher/getSubjectSelectList';
            	this.$http.post(url, {}).then(function(response) {
					if(response.data.success == true){
						
//						////console.log(response.data.data)
						self.subjectlist = response.data.data
						
						//第一次页面进来加载第一项
						if(self.subjectlist.length != 0){
							self.subjectselect = self.subjectlist[0].value;
							self.current = 1;
							self.getteacherlist();
						}
						
						self.subjectlistshow = false;
		            }else if(response.data.success == false){
		            		self.$Message.error({
								content: response.data.msg,
								duration: 10,
								closable: true
							});
		            		self.subjectlistshow = false;
		            	}
					})
					.catch(function(error) {
						self.$Message.error({
							content: '系统发生错误：错误码00071',
							duration: 10,
							closable: true
						});
						self.subjectlistshow = false;
					});
           	},
           	pageChange:function(value){
        		this.current = value;
        		this.getteacherlist()
        	},
           	getteacherlist:function(){
           		this.tableshow = true;
           		var self = this;
           		var senddata = { 
           			"current":this.current,
					"pageSize":"10",
					"subjectId":this.subjectselect,
					"level":"",
					"nameOrAccount":""
				}
           		if(this.searchInfo != ""){
        			senddata.nameOrAccount = this.searchInfo;
        		}
            	let url = api.apidomain+'/tqes/web/tchTeacher/getTeacherList';
            	this.$http.post(url,senddata).then(function(response) {
					if(response.data.success == true){
						
//						////console.log(response.data.data)
						self.data1 = response.data.data.items
						self.recordCount = response.data.data.recordCount
						self.tableshow = false;
		            }else if(response.data.success == false){
		            		self.$Message.error({
								content: response.data.msg,
								duration: 10,
								closable: true
							});	
		            		self.tableshow = false;
		            	}
					})
					.catch(function(error) {
						self.$Message.error({
							content: '系统发生错误：错误码00072',
							duration: 10,
							closable: true
						});
						self.tableshow = false;
					});
           	},
           	searchTeacher:function(){
           		this.current = 1;
				var senddata = { 
           			"current":this.current,
					"pageSize":"10",
					"subjectId":this.subjectselect,
					"level":"",
					"nameOrAccount":this.searchInfo
				}
           		var self = this;
            	let url = api.apidomain+'/tqes/web/tchTeacher/getTeacherList';
            	this.$http.post(url,senddata).then(function(response) {
					if(response.data.success == true){
						
//						console.log(response.data.data)
						self.data1 = response.data.data.items
						self.recordCount = response.data.data.recordCount
						self.tableshow = false;
		            }else if(response.data.success == false){
		            		self.$Message.error({
								content: response.data.msg,
								duration: 10,
								closable: true
							});	
		            		self.tableshow = false;
		            	}
					})
					.catch(function(error) {
						self.$Message.error({
							content: '系统发生错误：错误码00073',
							duration: 10,
							closable: true
						});
						self.tableshow = false;
					});
			},
           	delteacher:function(){
           		let delallid = [];
           		for(let key in this.selections){
           			delallid.push(this.selections[key].id)
           		}
           		if(delallid.length == 0){
           			this.$Message.error({
								content: '请勾选再删除',
								duration: 10,
								closable: true
							});	
           			return;
           		}
           		this.$Modal.confirm({
                    title: '确定删除',
                    content: '<p>确定删除这些老师信息吗</p>',
                    onOk: () => {
		               this.tableshow = true;
		           		var self = this;
		            	let url = api.apidomain+'/tqes/web/tchTeacher/batchDeleteTeacher';
		            	this.$http.post(url, {'ids':delallid}).then(function(response) {
							if(response.data.success == true){
								self.$Message.success({
				                    content: '删除成功',
				                    duration: 10,
				                    closable: true
		               			})
		//						////console.log(response.data.data)
								self.tableshow = false;
								self.current = 1;
								self.getteacherlist();
								self.selections = [];
				            }else if(response.data.success == false){
				            		self.$Message.error({
										content: response.data.msg,
										duration: 10,
										closable: true
									});	
				            		self.tableshow = false;
				            	}
							})
							.catch(function(error) {
								self.$Message.error({
									content: '系统发生错误：错误码00074',
									duration: 10,
									closable: true
								});
								self.tableshow = false;
							}); 
		                
                    },
                    onCancel: () => {
//                      this.$Message.info('Clicked cancel');
                    }
                });	
           	},
           	exportexl:function(){
			
				var senddata = {
					"subjectId":this.subjectselect,
				}
				
				var self = this;
            	let url = api.apidomain+'/tqes/web/tchTeacher/exportExcelDto';
            	this.$http.post(url,senddata).then(function(response) {
					if(response.data.success == true){
//						////console.log(response.data.data)
						window.location.href = response.data.data;
						self.loading = false;
	            }else if(response.data.success == false){
	            		self.loading = false;
	            		self.$Message.error({
								content: response.data.msg,
								duration: 10,
								closable: true
							});	
	            	}
	            	
				})
				.catch(function(error) {
					self.loading = false;
					self.$Message.error({
							content: '系统发生错误：错误码00075',
							duration: 10,
							closable: true
						});
				});
				
			}  
		},
		watch:{
			searchInfo:function(){
				this.searchTeacher();
			}
		}
		
	}
</script>

<style>
</style>